using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using ns20;

namespace Newtonsoft.Json.Converters
{
	public class StringEnumConverter : JsonConverter
	{
		private readonly Dictionary<Type, Class180<string, string>> dictionary_0 = new Dictionary<Type, Class180<string, string>>();

		[CompilerGenerated]
		private bool bool_0;

		[CompilerGenerated]
		private static Func<EnumMemberAttribute, string> func_0;

		public bool CamelCaseText
		{
			[CompilerGenerated]
			get
			{
				return bool_0;
			}
			[CompilerGenerated]
			set
			{
				bool_0 = value;
			}
		}

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			if (value == null)
			{
				writer.WriteNull();
				return;
			}
			Enum @enum = (Enum)value;
			string text = @enum.ToString("G");
			if (!char.IsNumber(text[0]) && text[0] != '-')
			{
				Class180<string, string> @class = method_0(@enum.GetType());
				string[] array = text.Split(',');
				for (int i = 0; i < array.Length; i++)
				{
					string text2 = array[i].Trim();
					@class.method_1(text2, out var gparam_);
					gparam_ = gparam_ ?? text2;
					if (CamelCaseText)
					{
						gparam_ = Class199.smethod_10(gparam_);
					}
					array[i] = gparam_;
				}
				string value2 = string.Join(", ", array);
				writer.WriteValue(value2);
			}
			else
			{
				writer.WriteValue(value);
			}
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			bool flag;
			Type type = ((flag = Class195.smethod_10(objectType)) ? Nullable.GetUnderlyingType(objectType) : objectType);
			if (reader.JsonToken_0 == JsonToken.Null)
			{
				if (!Class195.smethod_10(objectType))
				{
					throw JsonSerializationException.smethod_1(reader, "Cannot convert null value to {0}.".smethod_0(CultureInfo.InvariantCulture, objectType));
				}
				return null;
			}
			try
			{
				if (reader.JsonToken_0 == JsonToken.String)
				{
					string text = reader.Object_0.ToString();
					if (text == string.Empty && flag)
					{
						return null;
					}
					Class180<string, string> class180_ = method_0(type);
					string value;
					if (text.IndexOf(',') != -1)
					{
						string[] array = text.Split(',');
						for (int i = 0; i < array.Length; i++)
						{
							string string_ = array[i].Trim();
							array[i] = smethod_0(class180_, string_);
						}
						value = string.Join(", ", array);
					}
					else
					{
						value = smethod_0(class180_, text);
					}
					return Enum.Parse(type, value, ignoreCase: true);
				}
				if (reader.JsonToken_0 == JsonToken.Integer)
				{
					return Class182.smethod_10(reader.Object_0, CultureInfo.InvariantCulture, type);
				}
			}
			catch (Exception exception_)
			{
				throw JsonSerializationException.smethod_2(reader, "Error converting value {0} to type '{1}'.".smethod_1(CultureInfo.InvariantCulture, Class194.smethod_7(reader.Object_0), objectType), exception_);
			}
			throw JsonSerializationException.smethod_1(reader, "Unexpected token when parsing enum. Expected String or Integer, got {0}.".smethod_0(CultureInfo.InvariantCulture, reader.JsonToken_0));
		}

		private static string smethod_0(Class180<string, string> class180_0, string string_0)
		{
			class180_0.method_2(string_0, out var gparam_);
			return gparam_ ?? string_0;
		}

		private Class180<string, string> method_0(Type type_0)
		{
			if (!dictionary_0.TryGetValue(type_0, out var value))
			{
				lock (dictionary_0)
				{
					if (dictionary_0.TryGetValue(type_0, out value))
					{
						return value;
					}
					value = new Class180<string, string>(StringComparer.OrdinalIgnoreCase, StringComparer.OrdinalIgnoreCase);
					FieldInfo[] fields = type_0.GetFields();
					foreach (FieldInfo fieldInfo in fields)
					{
						string name = fieldInfo.Name;
						string text = (from EnumMemberAttribute enumMemberAttribute_0 in fieldInfo.GetCustomAttributes(typeof(EnumMemberAttribute), inherit: true)
							select enumMemberAttribute_0.Value).SingleOrDefault() ?? fieldInfo.Name;
						if (value.method_2(text, out var _))
						{
							throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".smethod_1(CultureInfo.InvariantCulture, text, type_0.Name));
						}
						value.method_0(name, text);
					}
					dictionary_0[type_0] = value;
					return value;
				}
			}
			return value;
		}

		public override bool CanConvert(Type objectType)
		{
			Type type_ = (Class195.smethod_10(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType);
			return type_.smethod_7();
		}

		[CompilerGenerated]
		private static string smethod_1(EnumMemberAttribute enumMemberAttribute_0)
		{
			return enumMemberAttribute_0.Value;
		}
	}
}
